package cn.tedu.v1.controller;


import cn.tedu.v1.model.Appointment;
import cn.tedu.v1.model.DepartmentOffice;
import cn.tedu.v1.model.Doctors;
import cn.tedu.v1.model.User;
import cn.tedu.v1.service.impl.AppointmentServiceImpl;
import cn.tedu.v1.service.impl.DepartmentOfficeServiceImpl;
import cn.tedu.v1.service.impl.DoctorsServiceImpl;
import cn.tedu.v1.vo.AppointmentVo;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;


import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 预约列表 前端控制器
 * </p>
 *
 * @author tedu.cn
 * @since 2021-07-06
 */
@RestController
@RequestMapping("/add_appointment")
@Slf4j
public class AppointmentController {

    /*获取当前用户,应该写在userController里*/
    @Autowired
    private AppointmentServiceImpl appointmentService;
    @GetMapping("")
    public User getUser(@AuthenticationPrincipal UserDetails userDetails){
        System.out.println("访问了controller");
       User user= appointmentService.selectUserByUserName(userDetails.getUsername());
        return user;
    }

    /*获取诊疗部门的所有科室*/
    @Resource
    private DepartmentOfficeServiceImpl departmentOfficeService;
    @GetMapping("/department")
    public List<DepartmentOffice> getTreatmentDepartment(){
        List<DepartmentOffice> treatmentDepartment=departmentOfficeService.treatmentDepartment();
        return treatmentDepartment;
    }

    /*根据接受到的科室id查询该部门的医生的*/
    @Resource
    private DoctorsServiceImpl doctorsService;
    @GetMapping("/loadDoctors")
    public List<Doctors> loadDoctors(Integer officeId){

        log.debug("offceId:"+officeId);
        if(officeId==null){
            String msg="请选择预约科室!";
            throw new SecurityException(msg);
        }
        List<Doctors> doctors=doctorsService.getDoctorsByOfficeId(officeId);
        log.debug("所有的医生"+doctors);
        return doctors;
    }

    /*添加预约*/
    @PostMapping("")
    public String addApointment(@Validated AppointmentVo appointmentVo, BindingResult result){
        log.debug("预约内容:{}",appointmentVo);
        String theTime=appointmentVo.getAppointtime();
        LocalDateTime appoinTime=LocalDateTime.parse(theTime);//得到预约时间的LocalDateTime对象
        String msg;
        if(result.hasErrors()){
            msg=result.getFieldError().getDefaultMessage();
            log.debug("所有的错误:{}",msg);
            return msg;
        }else if(appoinTime.isAfter(LocalDateTime.now().plusDays(7))){//如果预约时间大于7天
            msg="预约时间不可超出7天";
            return msg;
        } else {
            appointmentService.savaAppointment(appointmentVo);
            return "预约成功";
        }

    }

    /*获取所有预约内容*/
    @GetMapping("/getAppointments")
    public PageInfo<Appointment> getAppointmens(Integer pageNum,Integer pageSize){
        if(pageNum==null)
            pageNum=1;
        PageInfo<Appointment> pageInfo=appointmentService.getAppointments(pageNum,pageSize);
        return pageInfo;
    }

    /*修改一个预约的内容*/
    @PostMapping("/updateAppointment")
    public String updateAppointment(@Validated AppointmentVo appointmentVo,BindingResult result) {
        log.debug("修改的预约内容:{}",appointmentVo);
        String theTime=appointmentVo.getAppointtime();
        LocalDateTime appoinTime=LocalDateTime.parse(theTime);//得到预约时间的LocalDateTime对象

        String msg;
        if(result.hasErrors()){
            msg=result.getFieldError().getDefaultMessage();
            log.debug("错误信息:{}",msg);
            return msg;
        }else if(appoinTime.isAfter(LocalDateTime.now().plusDays(7))){//如果预约时间大于7天
            msg="预约时间不可超出7天";
            return msg;
        } else if(appointmentService.updateAppointment(appointmentVo)==1)  {
             return "修改成功";
        }
        return null;
    }

    //删除一个预约
    @GetMapping("deleteAppointment")
    public String deleteAppointment(Integer Id){
        log.debug("要删除的预约的id:"+Id);
        int num=appointmentService.deleteAppointment(Id);
        if(num==1)
            return "删除成功";
       return "服务器忙!";
    }

    @GetMapping("myappointment")
    public String getMyAppointment(@AuthenticationPrincipal UserDetails user){
        log.debug("当前登录的用户:{}",user);
        return user.toString();
    }


}
